package com.onpositive.wiki3.db.impl;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;

import com.onpositive.wiki3.db.catmodel.Category;
import com.onpositive.wiki3.db.impl.CountingVocabalary.Pair;

public class CategoryTemplateStore implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public static class CatTemplateInfo implements Serializable {
		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;
		double rate;
		int childCount;
		int subCatCount;
		String primaryTemplate;

		public CatTemplateInfo(double rate, int childCount,
				String primaryTemplate, int subCatCount2) {
			super();
			this.rate = rate;
			this.childCount = childCount;
			this.primaryTemplate = primaryTemplate;
			this.subCatCount = subCatCount2;
		}
	}

	protected HashMap<Integer, CatTemplateInfo> m = new HashMap<Integer, CategoryTemplateStore.CatTemplateInfo>();

	public void record(Category c, int childCount, int subCatCount,
			String primaryTemplate, double rate) {
		m.put(c.id(), new CatTemplateInfo(rate, childCount, primaryTemplate,
				subCatCount));
	}

	public void store(String string) throws FileNotFoundException, IOException {
		ObjectOutputStream objectOutputStream = new ObjectOutputStream(
				new BufferedOutputStream(new FileOutputStream(string)));
		objectOutputStream.writeObject(this);
		objectOutputStream.close();
	}

	public void read(String string) throws ClassNotFoundException, IOException {
		ObjectInputStream objectInputStream = new ObjectInputStream(
				new BufferedInputStream(new FileInputStream(string)));
		CategoryTemplateStore ts = (CategoryTemplateStore) objectInputStream
				.readObject();
		objectInputStream.close();
		this.m = ts.m;
	}

	public CatTemplateInfo get(Category q, int level) {
		CatTemplateInfo catTemplateInfo = m.get(q.id());
		if (catTemplateInfo == null && level < 5) {
			Category[] childCategories = q.getChildCategories();
			CountingVocabalary v = new CountingVocabalary();
			for (Category z : childCategories) {
				CatTemplateInfo catTemplateInfo2 = get(z, level + 1);
				if (catTemplateInfo2 != null) {
					v.add(catTemplateInfo2.primaryTemplate);
				} else {
					if (level == 0) {
						//System.out.println(z);
					}
				}
			}
			ArrayList<Pair> all = v.all();
			if (level == 0) {
				//System.out.println("a");
			}
			for (Pair p : all) {
				if (p.i >= childCategories.length / 2) {
					CatTemplateInfo t = new CatTemplateInfo((0.0 + p.i)
							/ childCategories.length, childCategories.length,
							p.s, childCategories.length);
					if (level < 2) {
						m.put(q.id(), t);
					}
					return t;
				}
			}
		}
		return catTemplateInfo;
	}
}
